function [negloglik] = log_det_B(hyps, func, n, n_class, X, y, approxF)
% a function evaluating log.det B
% where B = I + KW
% by Mark Norrish, 2011
% note: actually uses a bunch of other equations, esp. for derivations

dim = length(hyps) / n_class;
Hyps = reshape(hyps, dim, n_class);

bigK = zeros(n*n_class); K = zeros(n,n,n_class); sigma_noise = 0;1e-7;
for c = 1:n_class
  K(:,:,c) = func(Hyps(:,c), X, X) + sigma_noise*eye(n);
  bigK(1+(c-1)*n:c*n,1+(c-1)*n:c*n) = K(:,:,c);
end 

if nargin <= 6
  f = alg_3_3(n, n_class, K, y);
else
  f = alg_3_3(n, n_class, K, y, approxF);
end
%f = approxF;
F = reshape(f, n, n_class);
expsum = repmat(sum(exp(F)')',n_class,1);

pi = exp(f)./expsum;
bigPi = zeros(n_class*n,n);
for i = 1:n_class
  bigPi((i-1)*n+1:i*n,:) = diag(pi((i-1)*n+1:i*n));
end
W = diag(pi) - bigPi * bigPi';

negloglik = 0.5*log(det(eye(n*n_class) + bigK * W));